package com.industrial.operation.service.impl;

import com.industrial.common.core.utils.StringUtils;
import com.industrial.operation.domain.BigPingCompanyEvaluateInfo;
import com.industrial.operation.domain.daping.JdBaoGaoBaseData;
import com.industrial.operation.domain.gxhzb.OperationKfqInfo;
import com.industrial.operation.domain.gxhzb.OperationKfqScoreValue;
import com.industrial.operation.mapper.BigPingMapper;
import com.industrial.operation.mapper.OperationKfqScoreValueMapper;
import com.industrial.operation.service.IBigPingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BigPingServiceImpl implements IBigPingService {

    @Autowired
    private BigPingMapper bigPingMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询工业企业统计
     *
     * @return
     */
    @Override
    public Map<String, String> queryCompanyStatistics(String years) {

        Map<String,String> params = new HashMap<>(16);
        params.put("years",years);
        //查询企业总数
        BigPingCompanyEvaluateInfo companyTotalCount = bigPingMapper.queryCompanyStatistics(params);
        //查询规上企业数量
        params.put("companyType","1");
        BigPingCompanyEvaluateInfo gsCompanyCount = bigPingMapper.queryCompanyStatistics(params);
        //查询规下企业数量
        params.put("companyType","2");
        BigPingCompanyEvaluateInfo gxCompanyCount = bigPingMapper.queryCompanyStatistics(params);

        Map<String,String> resultMap = new HashMap<>(16);
        resultMap.put("companyTotal",companyTotalCount == null ? "0" : companyTotalCount.getCompanyCount());
        resultMap.put("gsCompanyCount",gsCompanyCount == null ? "0" : gsCompanyCount.getCompanyCount());
        resultMap.put("gxCompanyCount",gxCompanyCount  == null ? "0" : gxCompanyCount.getCompanyCount());
        System.out.println(resultMap);
        return resultMap;
    }

    /**
     * <p>
     *      查询城市的企业数量分布
     * </p>
     *
     * @return
     */
    @Override
    public List<Map<String,String>> queryCityCompanyCount(String years,String companyType) {
        List<BigPingCompanyEvaluateInfo> dbCount = bigPingMapper.queryCityCompanyCount(years,companyType);

        List<Map<String, String>> result = new ArrayList<>();
        if(dbCount != null && !dbCount.isEmpty()){
            result = dbCount.stream().map(k -> {
                Map<String, String> temp = new HashMap<>(16);
                temp.put("name", k.getTitle());
                temp.put("value", k.getCompanyCount());
                return temp;
            }).collect(Collectors.toList());
        }
        return result;
    }

    /**
     * <p>
     *      查询工业经济分析
     * </p>
     *
     * @return
     */
    @Override
    public Map<String, Object> queryIndustrialEconomicAnalysis(String years) {
        //查询营业收入
        Map<String,Object> operatingIncomeMap = bigPingMapper.queryOperatingIncome(years);
        return operatingIncomeMap;
    }

    /***
     * 各地市工业增加值运行分析
     * @return
     */
    @Override
    public Map<String,List<String>> queryCityIndustrialOutput(String years) {
        List<BigPingCompanyEvaluateInfo> bigPingCompanyEvaluateInfos = bigPingMapper.queryCityIndustrialOutput(years);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(BigPingCompanyEvaluateInfo temp : bigPingCompanyEvaluateInfos){
            String title = temp.getTitle();
            String companyCount = temp.getCompanyCount();
            xAxis.add(title);
            yAxis.add(companyCount);
        }
        Map<String,List<String>> resultMap = new HashMap<>(16);
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }

    /**
     * 工业季度报告
     *
     * @return
     */
    @Override
    public Map<String,String> queryGongYeJdBaoGao(String years) {

        Map<String,String> result = new HashMap<>(16);

        //查询工业季度报告的基础数据
        JdBaoGaoBaseData jdBaoGaoBaseData = bigPingMapper.JdBaoGaoBaseData(years);

        //查询全省的用地面积
        JdBaoGaoBaseData shanDongSumArea = bigPingMapper.queryShanDongSumArea(years);

        if(jdBaoGaoBaseData == null || shanDongSumArea == null){
            result.put("ydss","-");
            result.put("ydxssr","-");
            return result;
        }

        BigDecimal sumAreal = shanDongSumArea.getSumArea();
        //计算山东省的单位用地税收
        BigDecimal actualTax = jdBaoGaoBaseData.getActualTax();
        if(sumAreal == null || sumAreal.compareTo(BigDecimal.ZERO) == 0){
            result.put("ydss","-");
            result.put("ydxssr","-");
            return result;
        }else{
            BigDecimal ydss = actualTax.divide(sumAreal, 2, RoundingMode.HALF_UP);
            result.put("ydss",ydss.toString());
        }

        //计算山东省的单位用地销售收入
        BigDecimal mainIncome = jdBaoGaoBaseData.getMainIncome();
        BigDecimal ydxssr = mainIncome.divide(sumAreal, 2, RoundingMode.HALF_UP);
        result.put("ydxssr",ydxssr.toString());
        return result;
    }

    @Override
    public Map<String,Object> queryShiQiangXian(String years) {
        List<JdBaoGaoBaseData> jdBaoGaoBaseData = bigPingMapper.queryShiQiangXian(years);
        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(JdBaoGaoBaseData temp : jdBaoGaoBaseData){
            String city = temp.getCity();
            String district = temp.getDistrict();

            StringBuilder chineseName = new StringBuilder();
            if(StringUtils.isNotBlank(city)){
                String cityName = redisTemplate.opsForValue().get("area:" + city);
                chineseName.append(cityName).append("-");
            }

            if(StringUtils.isNotBlank(district)){
                String districtName = redisTemplate.opsForValue().get("area:" + district);
                chineseName.append(districtName);
            }
            xAxis.add(chineseName.toString());
            yAxis.add(temp.getTotalScore());
        }
        Map<String,Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }

    @Autowired
    private OperationKfqScoreValueMapper operationKfqScoreValueMapper;

    /**
     * 查询开发区考核
     *
     * @return
     */
    @Override
    public Map<String,Object> queryKfq(String years) {
        List<OperationKfqInfo> operationKfqInfos = bigPingMapper.queryKfq(years);
        List<String> xAxis = new ArrayList<>();
        List<BigDecimal> yAxis = new ArrayList<>();
        for(OperationKfqInfo t : operationKfqInfos){
            String dzn = t.getDzn();
            BigDecimal zf = t.getZf();
            xAxis.add(dzn);
            yAxis.add(zf);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }
}
